<!DOCTYPE html><html lang="zh-CN" data-theme="dark"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width,initial-scale=1"><title>虚拟Dom | 北城韩雨</title><meta name="keywords" content="虚拟Dom"><meta name="author" content="北城韩雨"><meta name="copyright" content="北城韩雨"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#0d0d0d"><meta name="description" content="虚拟Dom">
<meta property="og:type" content="article">
<meta property="og:title" content="虚拟Dom">
<meta property="og:url" content="http://example.com/2020/04/18/%E8%99%9A%E6%8B%9FDom/index.html">
<meta property="og:site_name" content="北城韩雨">
<meta property="og:description" content="虚拟Dom">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic.vjshi.com%2F2016-12-29%2F0604092c7129acd0f4029c6728db7fd7%2F00001.jpg%3Fx-oss-process%3Dstyle%2Fwatermark&refer=http%3A%2F%2Fpic.vjshi.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1629530724&t=333c5056a813a542419e5b661ebcfcd9">
<meta property="article:published_time" content="2020-04-17T16:00:00.000Z">
<meta property="article:modified_time" content="2019-06-02T16:00:00.000Z">
<meta property="article:author" content="北城韩雨">
<meta property="article:tag" content="虚拟Dom">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic.vjshi.com%2F2016-12-29%2F0604092c7129acd0f4029c6728db7fd7%2F00001.jpg%3Fx-oss-process%3Dstyle%2Fwatermark&refer=http%3A%2F%2Fpic.vjshi.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1629530724&t=333c5056a813a542419e5b661ebcfcd9"><link rel="shortcut icon" href="/img/favicon.png"><link rel="canonical" href="http://example.com/2020/04/18/%E8%99%9A%E6%8B%9FDom/"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = { 
  root: '/',
  algolia: undefined,
  localSearch: undefined,
  translate: undefined,
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'mediumZoom',
  Snackbar: undefined,
  source: {
    jQuery: 'https://cdn.jsdelivr.net/npm/jquery@latest/dist/jquery.min.js',
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/justifiedGallery/dist/js/jquery.justifiedGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/justifiedGallery/dist/css/justifiedGallery.min.css'
    },
    fancybox: {
      js: 'https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.js',
      css: 'https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isanchor: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: '虚拟Dom',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2019-06-03 00:00:00'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
    const detectApple = () => {
      if (GLOBAL_CONFIG_SITE.isHome && /iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
    })(window)</script><link rel="stylesheet" href="/css/custom.css"  media="defer" onload="this.media='all'"><link rel="stylesheet" href="/css/coin.css"  media="defer" onload="this.media='all'"><link rel="stylesheet" href="/css/liuxing.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/l-lin/font-awesome-animation/dist/font-awesome-animation.min.css"  media="defer" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/swiper/swiper-bundle.min.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/zhheo/JS-Heo@main/bb/showbb_in_index.css"><!-- hexo injector head_end start --><link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/4.1.1/animate.min.css" media="print" onload="this.media='screen'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/l-lin/font-awesome-animation/dist/font-awesome-animation.min.css" media="defer" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/hexo-butterfly-tag-plugins-plus/lib/tag_plugins.min.css" media="defer" onload="this.media='all'"><!-- hexo injector head_end end --><meta name="generator" content="Hexo 5.3.0"></head><body><div id="web_bg"></div><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="/img/my.jpg" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="site-data"><div class="data-item is-center"><div class="data-item-link"><a href="/archives/"><div class="headline">文章</div><div class="length-num">65</div></a></div></div><div class="data-item is-center"><div class="data-item-link"><a href="/tags/"><div class="headline">标签</div><div class="length-num">23</div></a></div></div><div class="data-item is-center"><div class="data-item-link"><a href="/categories/"><div class="headline">分类</div><div class="length-num">21</div></a></div></div></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/"><i class="fa-fw fas fa-home faa-tada"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/shuoshuo/"><i class="fa-fw fas fa-archive faa-tada"></i><span> 叨逼叨</span></a></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/tags/"><i class="fa-fw fas fa-tags faa-tada"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/categories/"><i class="fa-fw fas fa-folder-open faa-tada"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/link/"><i class="fa-fw fas fa-link faa-tada"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/comments/"><svg class="icon faa-tada" aria-hidden="true"><use xlink:href="#icon-message"></use></svg><span> 留言板</span></a></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/about/"><i class="fa-fw fas fa-heart faa-tada"></i><span> 个人简介</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic.vjshi.com%2F2016-12-29%2F0604092c7129acd0f4029c6728db7fd7%2F00001.jpg%3Fx-oss-process%3Dstyle%2Fwatermark&amp;refer=http%3A%2F%2Fpic.vjshi.com&amp;app=2002&amp;size=f9999,10000&amp;q=a80&amp;n=0&amp;g=0n&amp;fmt=jpeg?sec=1629530724&amp;t=333c5056a813a542419e5b661ebcfcd9')"><nav id="nav"><span id="blog_name"><a id="site-name" href="/">北城韩雨</a></span><div id="menus"><div class="menus_items"><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/"><i class="fa-fw fas fa-home faa-tada"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/shuoshuo/"><i class="fa-fw fas fa-archive faa-tada"></i><span> 叨逼叨</span></a></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/tags/"><i class="fa-fw fas fa-tags faa-tada"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/categories/"><i class="fa-fw fas fa-folder-open faa-tada"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/link/"><i class="fa-fw fas fa-link faa-tada"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/comments/"><svg class="icon faa-tada" aria-hidden="true"><use xlink:href="#icon-message"></use></svg><span> 留言板</span></a></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/about/"><i class="fa-fw fas fa-heart faa-tada"></i><span> 个人简介</span></a></div></div><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">虚拟Dom</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2020-04-17T16:00:00.000Z" title="发表于 2020-04-18 00:00:00">2020-04-18</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2019-06-02T16:00:00.000Z" title="更新于 2019-06-03 00:00:00">2019-06-03</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/%E8%99%9A%E6%8B%9FDom/">虚拟Dom</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-wordcount"><i class="far fa-file-word fa-fw post-meta-icon"></i><span class="post-meta-label">字数总计:</span><span class="word-count">7k</span><span class="post-meta-separator">|</span><i class="far fa-clock fa-fw post-meta-icon"></i><span class="post-meta-label">阅读时长:</span><span>26分钟</span></span><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="虚拟Dom"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><h1 id="虚拟DOM-Vdom"><a href="#虚拟DOM-Vdom" class="headerlink" title="虚拟DOM  Vdom"></a>虚拟DOM  Vdom</h1><h2 id="虚拟DOM-VDom-是什么"><a href="#虚拟DOM-VDom-是什么" class="headerlink" title="虚拟DOM(VDom)是什么"></a>虚拟DOM(VDom)是什么</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>简单来说就是用js对象的形式来描述真实的DOM</span><br><span class="line"><span class="number">2.</span>因为真实的DOM对象有很多的属性 所以我们每次操作真实的DOM的时候 都会伴随着这些属性的 性能会十分的低效</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//我们现在把真实的DOM属性都打印看了一下</span></span><br><span class="line">&lt;script&gt;</span><br><span class="line">    <span class="keyword">const</span> div = <span class="built_in">document</span>.createElement(<span class="string">&#x27;div&#x27;</span>)</span><br><span class="line">    <span class="keyword">let</span> res = <span class="string">&#x27;&#x27;</span></span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">var</span> key <span class="keyword">in</span> div)&#123;</span><br><span class="line">        res +=  key + <span class="string">&#x27; &#x27;</span></span><br><span class="line">    &#125;</span><br><span class="line">    <span class="built_in">console</span>.log(res)</span><br><span class="line">&lt;/script&gt;</span><br><span class="line"><span class="comment">//这里我们只是新建了一个div 还没有建立子元素 就看到如此多的属性 只要我们稍微操作一下DOM 浏览器的性能就会下降</span></span><br></pre></td></tr></table></figure>
<p><img src="1.png" alt=""></p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!--下面是一个我们真实的DOM元素--&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">div</span> <span class="attr">id</span>=<span class="string">&quot;app&quot;</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">p</span> <span class="attr">class</span>=<span class="string">&quot;item&quot;</span>&gt;</span>节点1<span class="tag">&lt;/<span class="name">p</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="javascript"><span class="comment">//然后我们采用js的形式去描述他 这样就是一个轻量级的虚拟DOM </span></span></span><br><span class="line"><span class="javascript"><span class="comment">//最外层元素是标签是DIV 属性都放在data里 子元素都放在children里 然后在依次去描述子元素</span></span></span><br><span class="line"><span class="javascript"><span class="comment">//这样我们在更新的时候 只需要找打新老两个虚拟DOM最小的修改去渲染页面 而不是像模板那样 统统重新渲染</span></span></span><br><span class="line"><span class="javascript">    <span class="function"><span class="title">createElment</span>(<span class="params"></span>)</span>&#123;</span></span><br><span class="line"><span class="javascript">        <span class="keyword">return</span>&#123;</span></span><br><span class="line"><span class="javascript">            tag:<span class="string">&#x27;div&#x27;</span>,</span></span><br><span class="line">            data:&#123;id:app&#125;,</span><br><span class="line"><span class="javascript">            children:[&#123;<span class="attr">tag</span>:<span class="string">&#x27;p&#x27;</span>,<span class="attr">data</span>:&#123;<span class="attr">class</span>:<span class="string">&#x27;item&#x27;</span>&#125;,<span class="attr">children</span>:[<span class="string">&#x27;节点1&#x27;</span>]&#125;]</span></span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br></pre></td></tr></table></figure>
<h2 id="如何新建虚拟DOM"><a href="#如何新建虚拟DOM" class="headerlink" title="如何新建虚拟DOM"></a>如何新建虚拟DOM</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>在react 和 Vue中 虚拟的DOM 的创建都是模板 和jsx 完成</span><br><span class="line"><span class="number">2.</span>模板到编译器 到render函数的转移 都是由webpack去工程化完成</span><br><span class="line"><span class="number">3.</span>下面我们要写一个最原始的虚拟DOM</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>首先我们要做标记 就是当你传进来我这个函数的每个标签或者文本 还有字节点 我都给他标记一下 它是什么类型</span><br><span class="line"><span class="number">2.</span>通过它的类型我们在去做额外的逻辑</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="comment">//新建虚拟DOM</span></span><br><span class="line"><span class="comment">//我们最后要生成的结果也就是我们的虚拟DOM</span></span><br><span class="line"><span class="comment">// &#123;</span></span><br><span class="line"><span class="comment">//     tag:&#x27;div&#x27;,</span></span><br><span class="line"><span class="comment">//     data:&#123;id:app&#125;,</span></span><br><span class="line"><span class="comment">//     children:[&#123;tag:&#x27;p&#x27;,data:&#123;class:&#x27;item&#x27;&#125;,children:[&#x27;节点1&#x27;]&#125;]</span></span><br><span class="line"><span class="comment">// &#125;</span></span><br><span class="line"><span class="comment">//定义下标记 比如元素的类型</span></span><br><span class="line"><span class="keyword">const</span> vnodeType =&#123; </span><br><span class="line">    HTML:<span class="string">&#x27;HTML&#x27;</span>, <span class="comment">//如果是元素 我就把你标记为HTML</span></span><br><span class="line">    TEXT:<span class="string">&#x27;TEXT&#x27;</span>, <span class="comment">//如果是文本 我就把你标记为TEXT</span></span><br><span class="line">    COMPONENT:<span class="string">&#x27;COMPONENT&#x27;</span>, <span class="comment">//如果是组件 我就把你标记为COMPONENT</span></span><br><span class="line">    CLASS_COMPONENT:<span class="string">&#x27;CLASS_COMPONENT&#x27;</span> <span class="comment">//如果是函数类型的组件 就标记为 CLASS_COMPONENT </span></span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//定义一下 你传进来的子元素 都是什么类型 比如一个 没有 多个</span></span><br><span class="line"><span class="keyword">const</span> childrenType = &#123;</span><br><span class="line">    EMPTY:<span class="string">&#x27;EMPTY&#x27;</span>, <span class="comment">//空的  你没有传子元素</span></span><br><span class="line">    SINGLE:<span class="string">&#x27;SINGLE&#x27;</span>, <span class="comment">//只有一个子元素</span></span><br><span class="line">    MULTIPLE: <span class="string">&#x27;MULTIPLE&#x27;</span> <span class="comment">//多个子元素</span></span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">createElement</span>(<span class="params">tag,data,children = <span class="literal">null</span></span>)</span>&#123;</span><br><span class="line">    <span class="keyword">let</span> flag </span><br><span class="line">    <span class="comment">//如果你给我传进来的是个字符串 那么我把你标记为HTML元素</span></span><br><span class="line">    <span class="keyword">if</span>(<span class="keyword">typeof</span> tag === <span class="string">&#x27;string&#x27;</span>)&#123;</span><br><span class="line">        <span class="comment">//我就把你标记为元素</span></span><br><span class="line">        flag = vnodeType.HTML</span><br><span class="line">    &#125;<span class="keyword">else</span> <span class="keyword">if</span>(<span class="keyword">typeof</span> tag === <span class="string">&#x27;function&#x27;</span>)&#123;</span><br><span class="line">        <span class="comment">//如果你传进来的是个 函数 我就认为你是组件</span></span><br><span class="line">        flag = vnodeType.COMPONENT</span><br><span class="line">    &#125;<span class="keyword">else</span>&#123;</span><br><span class="line">        <span class="comment">//如果什么都没有 我就认为你是一个文本</span></span><br><span class="line">        flag = vnodeType.TEXT</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">//标记子元素 分几种情况  第一种 没有子元素 第二种 有单个子元素 第三个 有多个子元素</span></span><br><span class="line">    <span class="keyword">let</span> childrenFlag</span><br><span class="line">    <span class="comment">//然后去标记我们的子元素 是哪种情况</span></span><br><span class="line">    <span class="keyword">if</span>(children == <span class="literal">null</span>)&#123;</span><br><span class="line">        <span class="comment">//如果你没传子元素 我就认为 你的children是空的</span></span><br><span class="line">        childrenFlag = childrenType.EMPTY</span><br><span class="line">    &#125;<span class="keyword">else</span> <span class="keyword">if</span>(<span class="built_in">Array</span>.isArray(children))&#123;</span><br><span class="line">        <span class="comment">//如果我你是个数组 我就看你的长度 有可能 你给我传个空数组过来 这时候我们也要标记是空的</span></span><br><span class="line">        <span class="keyword">let</span> length = children.length</span><br><span class="line">        <span class="keyword">if</span>(length === <span class="number">0</span>)&#123;</span><br><span class="line">            childrenFlag = childrenType.EMPTY</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            childrenFlag = childrenType.MULTIPLE</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;<span class="keyword">else</span> &#123;</span><br><span class="line">        <span class="comment">//如果你不是null 我就认为 你这里就是传进来了一个文本 </span></span><br><span class="line">        childrenFlag = childrenType.SINGLE</span><br><span class="line">        <span class="comment">//然后创建文本的vnode</span></span><br><span class="line">        children = createTextVnode(children + <span class="string">&#x27;&#x27;</span>)</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">//返回Vnode</span></span><br><span class="line">    <span class="keyword">return</span> &#123;</span><br><span class="line">        flag, <span class="comment">// 这个们用来标记 你是元素 还是 文本 还是 组件函数 之类的类型</span></span><br><span class="line">        tag,  <span class="comment">//标签 就是 div 和html5标签 如果是文本就没有tag 还由组件之类的</span></span><br><span class="line">        data, <span class="comment">// 属性</span></span><br><span class="line">        children, <span class="comment">// 子元素</span></span><br><span class="line">        childrenFlag <span class="comment">//标记子元素</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//创建文本类型的Vnode</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">createTextVnode</span> (<span class="params">text</span>)</span>&#123;</span><br><span class="line">    <span class="keyword">return</span> &#123;</span><br><span class="line">        flag:vnodeType.TEXT, <span class="comment">//文本类型</span></span><br><span class="line">        tag:<span class="literal">null</span>, <span class="comment">//因为是文本所以没有标签</span></span><br><span class="line">        data:<span class="literal">null</span>, <span class="comment">//也没有属性</span></span><br><span class="line">        children:text, <span class="comment">//子节点可能还是文本</span></span><br><span class="line">        childrenFlag: childrenType.EMPTY <span class="comment">//子节点文本类型是空</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//调用</span></span><br><span class="line">&lt;script&gt;</span><br><span class="line">    <span class="comment">//这里我们在Vue中 也是这么调用了 只不过那个是h的函数</span></span><br><span class="line">    <span class="keyword">let</span> div = createElement(<span class="string">&#x27;div&#x27;</span>,&#123;<span class="attr">id</span>:<span class="string">&#x27;test&#x27;</span>&#125;,[createElement(<span class="string">&#x27;p&#x27;</span>,&#123;&#125;,[<span class="string">&#x27;节点1&#x27;</span>])])</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="built_in">JSON</span>.stringify(div,<span class="literal">null</span>,<span class="number">2</span>))</span><br><span class="line">&lt;/script&gt;</span><br><span class="line"><span class="comment">//下面是我们自己处理完毕 生成的虚拟DOM</span></span><br></pre></td></tr></table></figure>
<p><img src="2.png" alt=""></p>
<h2 id="如何渲染虚拟DOM"><a href="#如何渲染虚拟DOM" class="headerlink" title="如何渲染虚拟DOM"></a>如何渲染虚拟DOM</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>首先我们会写一个render函数 </span><br><span class="line"><span class="number">2.</span>接收两个参数 第一个就是你的虚拟DOM 第二个就是要渲染在哪个容器里</span><br><span class="line"><span class="number">3.</span>我们在渲染的时候 可能每个节点 都会有自己的样式 或者点击事件 这些都在data里 所以我们在写render函数的时候也要考虑这些</span><br><span class="line"><span class="number">4.</span>在渲染的时候 我们需要区分一下 首次渲染 首次挂载元素 </span><br><span class="line"><span class="number">5.</span>在更新的时候 我们需要diff算法 比对新老虚拟DOM 在去挂载到页面</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//刚才我们已经通过createElement函数生成了虚拟DOM</span></span><br><span class="line">    <span class="comment">//这些虚拟DOM 的节点上可能还有一些属性 </span></span><br><span class="line">    <span class="keyword">let</span> vnode = createElement(<span class="string">&#x27;div&#x27;</span>,&#123;<span class="attr">id</span>:<span class="string">&#x27;test&#x27;</span>&#125;,[</span><br><span class="line">        createElement(<span class="string">&#x27;p&#x27;</span>,&#123;<span class="attr">key</span>:<span class="string">&#x27;a&#x27;</span>,<span class="attr">sytle</span>:&#123;<span class="attr">color</span>:<span class="string">&#x27;blue&#x27;</span>&#125;&#125;,[<span class="string">&#x27;节点1&#x27;</span>]),</span><br><span class="line">        createElement(<span class="string">&#x27;p&#x27;</span>,&#123;<span class="attr">key</span>:<span class="string">&#x27;b&#x27;</span>,<span class="string">&#x27;@click&#x27;</span>:<span class="function">() =&gt;</span> &#123;alert(<span class="string">&#x27;xx&#x27;</span>)&#125;&#125;,[<span class="string">&#x27;节点2&#x27;</span>]),</span><br><span class="line">        createElement(<span class="string">&#x27;p&#x27;</span>,&#123;<span class="attr">key</span>:<span class="string">&#x27;c&#x27;</span>,<span class="string">&#x27;class&#x27;</span>:<span class="string">&#x27;item-header&#x27;</span>&#125;,[<span class="string">&#x27;节点3&#x27;</span>]),</span><br><span class="line">        createElement(<span class="string">&#x27;p&#x27;</span>,&#123;<span class="attr">key</span>:<span class="string">&#x27;d&#x27;</span>&#125;,[<span class="string">&#x27;节点4&#x27;</span>])</span><br><span class="line">    ])</span><br><span class="line">    <span class="comment">//在这里我们需要用render函数 把他们 都渲染到页面上</span></span><br><span class="line">    render(vnode,<span class="built_in">document</span>.querySelector(<span class="string">&#x27;#app&#x27;</span>))</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//render函数 我们要做的就是把虚拟DOM 渲染到页面上 这个时候 需要区分一下首次渲染 和更新之后的渲染</span></span><br><span class="line"><span class="comment">//渲染函数</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">render</span> (<span class="params">vnode,container</span>) </span>&#123;</span><br><span class="line"><span class="comment">//挂载函数mount</span></span><br><span class="line">    mount()</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// mount函数 要区分一下 你的vode元素还事文本 然后分别调用不同的挂载方法</span></span><br><span class="line"><span class="comment">//首次挂载 把我们的真实DOM挂载到容器上</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">mount</span> (<span class="params">vnode,container</span>)</span>&#123;</span><br><span class="line">    <span class="comment">//先区分flag的类型 这样就知道 你每个节点到底是什么东西</span></span><br><span class="line">    <span class="keyword">let</span> &#123;flg&#125; = vnode</span><br><span class="line">    <span class="keyword">if</span>(flag === vnodeType.HTML) &#123;</span><br><span class="line">        <span class="comment">//如果你是元素 我们就用元素的挂载方法</span></span><br><span class="line">        mountElement(vnode,container)</span><br><span class="line">    &#125;<span class="keyword">else</span> <span class="keyword">if</span>(flag === vnodeType.TEXT)&#123;</span><br><span class="line">        <span class="comment">//如果你是文本 我们就用文本的挂载方法</span></span><br><span class="line">        mountText(vnode,container)</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//mountElement函数 挂载元素</span></span><br><span class="line"><span class="comment">//如果标记的是元素的挂载方法</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">mountElement</span>(<span class="params">vnode,container</span>)</span>&#123;</span><br><span class="line">    <span class="comment">//创建你虚拟DOM 的标签元素</span></span><br><span class="line">    <span class="keyword">let</span> dom = <span class="built_in">document</span>.createElement(vnode.tag)</span><br><span class="line">    <span class="comment">//存储真实DOM 为挂载用</span></span><br><span class="line">    vnode.el = dom</span><br><span class="line">    <span class="comment">//把它上面的属性读解析出来</span></span><br><span class="line">    <span class="comment">// let data = vnode.data</span></span><br><span class="line">    <span class="comment">//把虚拟DOM 的属性解构出来</span></span><br><span class="line">    <span class="keyword">let</span> &#123;data,children,childrenFlag&#125; = vnode</span><br><span class="line">    <span class="comment">//开始处理节点的属性</span></span><br><span class="line">    <span class="keyword">for</span>(<span class="keyword">var</span> key <span class="keyword">in</span> data)&#123;</span><br><span class="line">        <span class="comment">//这里我们分辨把当前的dom 和 绑定的key 还有新的节点 和你的值传入进去 这里要做对比</span></span><br><span class="line">        patchData(dom,key,<span class="literal">null</span>,data[key])</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">//开始挂载子元素</span></span><br><span class="line">    <span class="comment">//首先判断你的子元素是什么类型</span></span><br><span class="line">    <span class="keyword">if</span>(childrenFlag != childrenType.EMPTY)&#123;</span><br><span class="line">        <span class="comment">//需要判断一下你的子节点类型如果不是空的我才能挂载</span></span><br><span class="line">        <span class="keyword">if</span>(childrenFlag == childrenType.SINGLE)&#123;</span><br><span class="line">            <span class="comment">//如果你是文本节点 我们就直接使用mount方法去挂载 到我们的父元素上 这里就不是容器了</span></span><br><span class="line">            mount(children,dom)</span><br><span class="line">        &#125;<span class="keyword">else</span> <span class="keyword">if</span>(childrenFlag == childrenType.MULTIPLE)&#123;</span><br><span class="line">            <span class="comment">//如果你的子元素是多个 也就是传进来是个数组 那我们就开始循环</span></span><br><span class="line">            <span class="keyword">for</span>(<span class="keyword">var</span> i = <span class="number">0</span>; i &lt; children.length;i++)&#123;</span><br><span class="line">                <span class="comment">//一次去挂载</span></span><br><span class="line">                mount(children[i],dom)</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">//最后挂载我们的父元素</span></span><br><span class="line">    container.appendChild(dom)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//mountText 方法 挂载文本</span></span><br><span class="line"><span class="comment">//如果标记是文本挂载方法</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">mountText</span> (<span class="params">vnode,container</span>)</span>&#123;</span><br><span class="line">    <span class="keyword">let</span> dom = <span class="built_in">document</span>.createTextNode(vnode.children)</span><br><span class="line">    vnode.el = dom</span><br><span class="line">    <span class="comment">//把文本节点挂载到你的父亲节点上</span></span><br><span class="line">    container.appendChild(dom)</span><br><span class="line">&#125;   </span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">如下所见 我们已经把文本都生成的虚拟DOM 都挂载到了真实的DOM上</span><br><span class="line"><span class="comment">//刚才我们已经通过createElement函数生成了虚拟DOM</span></span><br><span class="line">    <span class="comment">//这些虚拟DOM 的节点上可能还有一些属性 </span></span><br><span class="line">    <span class="keyword">let</span> vnode = createElement(<span class="string">&#x27;div&#x27;</span>,&#123;<span class="attr">id</span>:<span class="string">&#x27;test&#x27;</span>&#125;,[</span><br><span class="line">        createElement(<span class="string">&#x27;p&#x27;</span>,&#123;<span class="attr">key</span>:<span class="string">&#x27;a&#x27;</span>,<span class="attr">sytle</span>:&#123;<span class="attr">color</span>:<span class="string">&#x27;blue&#x27;</span>&#125;&#125;,<span class="string">&#x27;节点1&#x27;</span>),</span><br><span class="line">        createElement(<span class="string">&#x27;p&#x27;</span>,&#123;<span class="attr">key</span>:<span class="string">&#x27;b&#x27;</span>,<span class="string">&#x27;@click&#x27;</span>:<span class="function">() =&gt;</span> &#123;alert(<span class="string">&#x27;xx&#x27;</span>)&#125;&#125;,<span class="string">&#x27;节点2&#x27;</span>),</span><br><span class="line">        createElement(<span class="string">&#x27;p&#x27;</span>,&#123;<span class="attr">key</span>:<span class="string">&#x27;c&#x27;</span>,<span class="string">&#x27;class&#x27;</span>:<span class="string">&#x27;item-header&#x27;</span>&#125;,<span class="string">&#x27;节点3&#x27;</span>),</span><br><span class="line">        createElement(<span class="string">&#x27;p&#x27;</span>,&#123;<span class="attr">key</span>:<span class="string">&#x27;d&#x27;</span>&#125;,<span class="string">&#x27;节点4&#x27;</span>)</span><br><span class="line">    ])</span><br><span class="line">    </span><br><span class="line">    <span class="comment">//在这里我们需要用render函数 把他们 都渲染到页面上</span></span><br><span class="line">    <span class="built_in">setTimeout</span>(<span class="function">() =&gt;</span> &#123;</span><br><span class="line">        render(vnode,<span class="built_in">document</span>.querySelector(<span class="string">&#x27;#app&#x27;</span>))</span><br><span class="line">    &#125;,<span class="number">1000</span>)</span><br></pre></td></tr></table></figure>
<p><img src="1.gif" alt=""></p>
<h2 id="处理节点的属性"><a href="#处理节点的属性" class="headerlink" title="处理节点的属性"></a>处理节点的属性</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>我们的虚拟DOM上 还有一些属性</span><br><span class="line"><span class="number">2.</span>比如样式 事件</span><br><span class="line"><span class="number">3.</span>这些属性的处理 我们也是在 render下的 mountElment函数里完成的</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//patchData 函数</span></span><br><span class="line"><span class="comment">//处理节点上的属性</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">patchData</span> (<span class="params">el,key,prv,next</span>) </span>&#123;</span><br><span class="line">    <span class="comment">//这里就是用switch 一次去比对了</span></span><br><span class="line">    <span class="keyword">switch</span>(key)&#123;</span><br><span class="line">        <span class="keyword">case</span> <span class="string">&#x27;style&#x27;</span>:</span><br><span class="line">            <span class="keyword">for</span>(<span class="keyword">let</span> k <span class="keyword">in</span> next)&#123;</span><br><span class="line">                <span class="built_in">console</span>.log(next)</span><br><span class="line">                <span class="comment">//因为你虚拟DOM传进来的 &#123;style:&#123;color:blue&#125;&#125; 处理之后 &lt;div style=&quot;color:blue&quot;&gt;</span></span><br><span class="line">                el.style[k]= next[k]</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">break</span>;</span><br><span class="line">        <span class="keyword">case</span> <span class="string">&#x27;class&#x27;</span>:</span><br><span class="line">            el.className = next </span><br><span class="line">        <span class="keyword">break</span>;</span><br><span class="line">         <span class="comment">//如果默认的就是那些有事件的</span></span><br><span class="line">        <span class="keyword">default</span>:</span><br><span class="line">            <span class="comment">//我们的key循环出来可能是这样的@click字符串 取它的0位 就是@</span></span><br><span class="line">            <span class="keyword">if</span>(key[<span class="number">0</span>] === <span class="string">&#x27;@&#x27;</span>)&#123;</span><br><span class="line">                <span class="comment">//给我们的当前DOM添加事件 </span></span><br><span class="line">                el.addEventListener(key.slice(<span class="number">1</span>),next)</span><br><span class="line">            &#125;<span class="keyword">else</span> &#123;</span><br><span class="line">                <span class="comment">//如果不是你自己定义的 我们就添加自定义属性 </span></span><br><span class="line">                el.setAttribute(key,next)</span><br><span class="line">            &#125;</span><br><span class="line">        <span class="keyword">break</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="render函数的渲染逻辑"><a href="#render函数的渲染逻辑" class="headerlink" title="render函数的渲染逻辑"></a>render函数的渲染逻辑</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>首先我们在创建虚拟DOM的时候 就已经把每个节点的类型取标记好了</span><br><span class="line"><span class="number">2.</span>然后我们根据你的节点类型 去判断你是标签 还是 文本 如果是标签就执行标签的挂载逻辑 如果是文本就执行文本的挂载逻辑</span><br><span class="line"><span class="number">3.</span>如果是标签 我们这里还需要把你的属性都做下挂载 挂载逻辑就是把你当前的DOM传进去 然后 你属性里所有的key 和对应的值 还有 你变化后的都是什么传进去 然后开始执行判断之类的语句</span><br><span class="line"><span class="number">4.</span>如果你有事节点 我们就直接把你当前的文本节点的值传进去直接挂载就可以</span><br><span class="line"><span class="number">5.</span>如果还有子元素 我们需要在挂载元素的函数里在去判断你子元素的类型 然后在一次的去使用mount函数挂载</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//完成的render函数</span></span><br><span class="line"><span class="comment">//渲染函数</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">render</span> (<span class="params">vnode,container</span>) </span>&#123;</span><br><span class="line">    <span class="built_in">console</span>.log(container)</span><br><span class="line">    mount(vnode,container)</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//首次挂载 把我们的真实DOM挂载到容器上</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">mount</span> (<span class="params">vnode,container</span>)</span>&#123;</span><br><span class="line">    <span class="comment">//先区分flag的类型 这样就知道 你每个节点到底是什么东西</span></span><br><span class="line">    <span class="keyword">let</span> &#123;flag&#125; = vnode</span><br><span class="line">    <span class="keyword">if</span>(flag === vnodeType.HTML) &#123;</span><br><span class="line">        <span class="comment">//如果你是元素 我们就用元素的挂载方法</span></span><br><span class="line">        mountElement(vnode,container)</span><br><span class="line">    &#125;<span class="keyword">else</span> <span class="keyword">if</span>(flag === vnodeType.TEXT)&#123;</span><br><span class="line">        <span class="comment">//如果你是文本 我们就用文本的挂载方法</span></span><br><span class="line">        mountText(vnode,container)</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//如果标记的是元素的挂载方法</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">mountElement</span>(<span class="params">vnode,container</span>)</span>&#123;</span><br><span class="line">    <span class="comment">//创建你虚拟DOM 的标签元素</span></span><br><span class="line">    <span class="keyword">let</span> dom = <span class="built_in">document</span>.createElement(vnode.tag)</span><br><span class="line">    <span class="comment">//存储真实DOM 为挂载用</span></span><br><span class="line">    vnode.el = dom</span><br><span class="line">    <span class="comment">//把它上面的属性读解析出来</span></span><br><span class="line">    <span class="comment">// let data = vnode.data</span></span><br><span class="line">    <span class="comment">//把虚拟DOM 的属性解构出来</span></span><br><span class="line">    <span class="keyword">let</span> &#123;data,children,childrenFlag&#125; = vnode</span><br><span class="line">    <span class="comment">//开始处理节点的属性</span></span><br><span class="line">    <span class="keyword">for</span>(<span class="keyword">var</span> key <span class="keyword">in</span> data)&#123;</span><br><span class="line">        <span class="comment">//这里我们分辨把当前的dom 和 绑定的key 还有新的节点 和你的值传入进去 这里要做对比</span></span><br><span class="line">        patchData(dom,key,<span class="literal">null</span>,data[key])</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">//开始挂载子元素</span></span><br><span class="line">    <span class="comment">//首先判断你的子元素是什么类型</span></span><br><span class="line">    <span class="keyword">if</span>(childrenFlag != childrenType.EMPTY)&#123;</span><br><span class="line">        <span class="comment">//需要判断一下你的子节点类型如果不是空的我才能挂载</span></span><br><span class="line">        <span class="keyword">if</span>(childrenFlag == childrenType.SINGLE)&#123;</span><br><span class="line">            <span class="comment">//如果你是文本节点 我们就直接使用mount方法去挂载 到我们的父元素上 这里就不是容器了</span></span><br><span class="line">            mount(children,dom)</span><br><span class="line">        &#125;<span class="keyword">else</span> <span class="keyword">if</span>(childrenFlag == childrenType.MULTIPLE)&#123;</span><br><span class="line">            <span class="comment">//如果你的子元素是多个 也就是传进来是个数组 那我们就开始循环</span></span><br><span class="line">            <span class="keyword">for</span>(<span class="keyword">var</span> i = <span class="number">0</span>; i &lt; children.length;i++)&#123;</span><br><span class="line">                <span class="comment">//一次去挂载</span></span><br><span class="line">                mount(children[i],dom)</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">//最后挂载我们的父元素</span></span><br><span class="line">    container.appendChild(dom)</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//如果标记是文本挂载方法</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">mountText</span> (<span class="params">vnode,container</span>)</span>&#123;</span><br><span class="line">    <span class="keyword">let</span> dom = <span class="built_in">document</span>.createTextNode(vnode.children)</span><br><span class="line">    vnode.el = dom</span><br><span class="line">    container.appendChild(dom)</span><br><span class="line">&#125;   </span><br><span class="line"><span class="comment">//处理节点上的属性</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">patchData</span> (<span class="params">el,key,prv,next</span>) </span>&#123;    </span><br><span class="line">    <span class="comment">//这里就是用switch 一次去比对了</span></span><br><span class="line">    <span class="comment">// console.log(next)</span></span><br><span class="line">    <span class="comment">// console.log(next[key])</span></span><br><span class="line">    <span class="keyword">switch</span>(key)&#123;</span><br><span class="line">        <span class="keyword">case</span> <span class="string">&#x27;style&#x27;</span>:</span><br><span class="line">            <span class="keyword">for</span>(<span class="keyword">let</span> k <span class="keyword">in</span> next)&#123;</span><br><span class="line">                <span class="built_in">console</span>.log(next)</span><br><span class="line">                <span class="comment">//因为你虚拟DOM传进来的 &#123;style:&#123;color:blue&#125;&#125; 处理之后 &lt;div style=&quot;color:blue&quot;&gt;</span></span><br><span class="line">                el.style[k]= next[k]</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">break</span>;</span><br><span class="line">        <span class="keyword">case</span> <span class="string">&#x27;class&#x27;</span>:</span><br><span class="line">            el.className = next </span><br><span class="line">        <span class="keyword">break</span>;</span><br><span class="line">         <span class="comment">//如果默认的就是那些有事件的</span></span><br><span class="line">        <span class="keyword">default</span>:</span><br><span class="line">            <span class="comment">//我们的key循环出来可能是这样的@click字符串 取它的0位 就是@</span></span><br><span class="line">            <span class="keyword">if</span>(key[<span class="number">0</span>] === <span class="string">&#x27;@&#x27;</span>)&#123;</span><br><span class="line">                <span class="comment">//给我们的当前DOM添加事件 </span></span><br><span class="line">                el.addEventListener(key.slice(<span class="number">1</span>),next)</span><br><span class="line">            &#125;<span class="keyword">else</span> &#123;</span><br><span class="line">                <span class="comment">//如果不是你自己定义的 我们就添加自定义属性 </span></span><br><span class="line">                el.setAttribute(key,next)</span><br><span class="line">            &#125;</span><br><span class="line">        <span class="keyword">break</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="更新patch-diff"><a href="#更新patch-diff" class="headerlink" title="更新patch diff"></a>更新patch diff</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>当我们的虚拟DOM发生变化的时候 我们需要重新的去生成虚拟DOM 然后去挂载在视图上</span><br><span class="line"><span class="number">2.</span>更新的过程分为很多中情况 比如你的节点 有的是删除操作  有的是 移动操作 有的是增加操作</span><br><span class="line"><span class="number">3.</span>所以我们需要根据这些不同的情况去做不同的操作 </span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//我们首次渲染是执行mount方法 在你的真实的DOM里也就是根 是根本没有vnode 所以这里我们要跟据 你是首次渲染 还是更新 在render函数里去做判断</span></span><br><span class="line"><span class="comment">//渲染函数</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">render</span> (<span class="params">vnode,container</span>) </span>&#123; </span><br><span class="line">    <span class="keyword">if</span>(container.vnode)&#123;</span><br><span class="line">        <span class="comment">//如果你的vnode存在你的DOM里就说明 你首次挂载过</span></span><br><span class="line">        <span class="comment">//参数是 老的vnode 新的vnode 你的容器</span></span><br><span class="line">        patch(container.vnode,vnode,container)</span><br><span class="line">    &#125;<span class="keyword">else</span>&#123;</span><br><span class="line">        <span class="comment">//如果没有 我们就说明你是首次渲染</span></span><br><span class="line">        mount(vnode,container)</span><br><span class="line">    &#125;</span><br><span class="line">    container.vnode = vnode</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<h2 id="patch方法"><a href="#patch方法" class="headerlink" title="patch方法"></a>patch方法</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>主要是用来做更新用的</span><br><span class="line"><span class="number">2.</span>我们首次进来的时候只有一个vnode 会把它存在容器中</span><br><span class="line"><span class="number">3.</span>当产生新的vnode的时候 我们就开始patch更新</span><br><span class="line"><span class="number">4.</span>patch的方法分三种情况 第一种是你老的vnode的节点类型 和新的不相等 也就老的是标签 新的是文本 那就直接替换掉</span><br><span class="line">如果老的是元素新的也是元素那么就开始比对 第三种 如果老的直接就是个文本 就直接的把原来的删除掉 把文本重新挂载上</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//更新 patch</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">patch</span> (<span class="params">prev,next,container</span>)</span>&#123;</span><br><span class="line">    <span class="comment">//参数是 老的vnode 新的vnode 和同期</span></span><br><span class="line">    <span class="comment">//把我们新老的标记 解构出来</span></span><br><span class="line">    <span class="keyword">let</span> nextFlag = next.flag</span><br><span class="line">    <span class="keyword">let</span> prevFlag = prev.flag</span><br><span class="line">    <span class="comment">//如果你新的vnode和老的vnode 标记的类型不一样 就说明没什么而更新操作 </span></span><br><span class="line">    <span class="comment">//比如 你老的是标签 你新的是文本 就直接干掉 然后重新渲染即可</span></span><br><span class="line">    <span class="keyword">if</span>(nextFlag !== prevFlag)&#123;</span><br><span class="line">        replaceVnode(prev,next,container)</span><br><span class="line">    &#125;<span class="keyword">else</span> <span class="keyword">if</span>(nextFlag == vnodeType.HTML)&#123;</span><br><span class="line">        <span class="comment">//如果你新节点传进来是个标签 我们就做下面的操作</span></span><br><span class="line">        patchElement(prev,next,container)</span><br><span class="line">    &#125;<span class="keyword">else</span> <span class="keyword">if</span>(nextFlag == vnodeType.TEXT)&#123;</span><br><span class="line">        <span class="comment">//如果你新的虚拟DOM 是个文本</span></span><br><span class="line">        patchText(prev,next,container)</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="replaceVnode方法"><a href="#replaceVnode方法" class="headerlink" title="replaceVnode方法"></a>replaceVnode方法</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 如果你新的vnode 和老的vnode的节点标记类型都不一样 那么没有什么算法 我们就直接替换</span></span><br><span class="line"><span class="comment">//如果你新的vnode和老的vnode 标记的类型不一样 就说明没什么而更新操作 </span></span><br><span class="line"><span class="comment">//比如 你老的是标签 你新的是文本 就直接干掉 然后重新渲染即可</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">replaceVnode</span> (<span class="params">prev,next,container</span>)</span>&#123;</span><br><span class="line">    <span class="comment">//就直接删除 然后在挂载新的 这里的prev.el 是我们最开始存放的就是根的真实的DOM</span></span><br><span class="line">    container.removeChild(prev.el)</span><br><span class="line">    <span class="comment">//然后在挂载新的</span></span><br><span class="line">    mount(next,container)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="patchElement方法"><a href="#patchElement方法" class="headerlink" title="patchElement方法"></a>patchElement方法</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>如果你新的标签 老的也是标签 那么我就看看是不是统一标签 如果不是 就直接调用 replaceVnode </span><br><span class="line"><span class="number">2.</span>如果老的vnode 和 新的vnode 都是标签元素而且相同 那么们就开始执行属性的操作 该更新更新该删除删除</span><br><span class="line"><span class="number">3.</span>然后开始对比子元素</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//如果你新的传进来的也是个标签</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">patchElement</span>(<span class="params">prev,next,container</span>) </span>&#123;</span><br><span class="line">    <span class="comment">//我们就看看 你新的vnode 和老的vnode 标签是不是一样的 如果不是一样的就直接干点</span></span><br><span class="line">    <span class="keyword">if</span>(prev.tag !== next.tag)&#123;</span><br><span class="line">        <span class="comment">//不是一样的标签 就直接干调才重新挂载</span></span><br><span class="line">        replaceVnode(next,container)</span><br><span class="line">        <span class="keyword">return</span></span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">//如果不是那种情况我们就开始处理一下属性</span></span><br><span class="line">    <span class="keyword">let</span> el = (next.el = prev.el) <span class="comment">//更新一下el</span></span><br><span class="line">    <span class="keyword">let</span> prevData = prev.data <span class="comment">//把老的vnode属性解构出来</span></span><br><span class="line">    <span class="keyword">let</span> nextData = next.data <span class="comment">//把新的vnode属性解构出来</span></span><br><span class="line">    <span class="keyword">if</span>(nextData)&#123;</span><br><span class="line">        <span class="comment">//如果新的vnode 有属性 我们就开始遍历更新</span></span><br><span class="line">        <span class="keyword">for</span>(<span class="keyword">let</span> key <span class="keyword">in</span> nextData)&#123;</span><br><span class="line">            <span class="comment">//这里就是获取到如果你的新的vnode里也有这个key 老的vnode也有这个key之不是value不一样所以我们要获取出来</span></span><br><span class="line">            <span class="comment">//新&#123;key:a&#125; //老&#123;key:b&#125;都存在相同的key 只不过值不一样 我们要取出来做对比</span></span><br><span class="line">            <span class="keyword">let</span> prevVal = prevData[key]</span><br><span class="line">            <span class="keyword">let</span> nextVal = nextData[key]</span><br><span class="line">            <span class="comment">//开始更新 根节点 然后key 老的属性值 新的属性值</span></span><br><span class="line">            patchData(el,key,prevVal,nextVal)</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">if</span>(prevData)&#123;</span><br><span class="line">        <span class="comment">//如果老的vnode存在 </span></span><br><span class="line">        <span class="keyword">for</span>(<span class="keyword">let</span> key <span class="keyword">in</span> prevData)&#123;</span><br><span class="line">            <span class="comment">//把值搞出来</span></span><br><span class="line">            <span class="keyword">let</span> prevVal = prevData[key]</span><br><span class="line">            <span class="keyword">if</span>(prevVal &amp;&amp; !nextData.hasOwnProperty(key))&#123;</span><br><span class="line">                <span class="comment">//如果你老的vnode属性值存在 并且 在新的vnode没有这个属性 那么我们就该做删除操作 </span></span><br><span class="line">                patchData(el,key,prevVal,<span class="literal">null</span>)</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">//上面的属性更新完毕了 </span></span><br><span class="line">    <span class="comment">//下面开始更新子元素</span></span><br><span class="line">    <span class="comment">//需要传进去的参数 老vnode的子元素的节点类型 和 子元素 新vnode的子元素类型和子元素 还有真实dom</span></span><br><span class="line">    patchChildren(prev.childrenFlag,next.childrenFlag,prev.children,next.children,el)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="patchChildren方法"><a href="#patchChildren方法" class="headerlink" title="patchChildren方法"></a>patchChildren方法</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//更新patch子元素 比较复杂 需要多种的情况</span></span><br><span class="line"><span class="comment">//开始更新子元素节点</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">patchChildren</span>(<span class="params">prevChilFlag,nextChildFlag,prevChildren,nextChildren,container</span>)</span>&#123;</span><br><span class="line">    <span class="comment">//这里面会针对新老几种状况取做对比 和操作</span></span><br><span class="line">    <span class="comment">// 1.老的vnode的子元素 是单独的 比如就是一个文本</span></span><br><span class="line">    <span class="comment">// 2.老的vnode的子元素 是空的 没有</span></span><br><span class="line">    <span class="comment">// 3.老的vnode子元素 是多个的 </span></span><br><span class="line">    <span class="comment">//然后上面的每一个种情况都要对比下面新的哪三种情况</span></span><br><span class="line">    <span class="comment">// 1.新的vnode子元素是单独的</span></span><br><span class="line">    <span class="comment">// 2.新的vnode子元素是空的</span></span><br><span class="line">    <span class="comment">// 3.新的vnode子元素是多个</span></span><br><span class="line">    <span class="keyword">switch</span>(prevChilFlag)&#123;</span><br><span class="line">        <span class="comment">//老的虚拟DOM 是单个的 然后分别取对比新的vnode那三种情况</span></span><br><span class="line">        <span class="keyword">case</span> childrenType.SINGLE:</span><br><span class="line">            <span class="comment">//这里我们嵌套了一层switch  对应 新的vnode 三种情况</span></span><br><span class="line">            <span class="keyword">switch</span>(nextChildFlag)&#123;</span><br><span class="line">                <span class="comment">// 老的vnode是单独的 对应新的vnode 是拿单独的</span></span><br><span class="line">                <span class="keyword">case</span> childrenType.SINGLE:</span><br><span class="line">                    <span class="comment">//老的vnode子元素值文本 新的也是 就直接把老的文本删除 然后从新mount挂载新的</span></span><br><span class="line">                    patch(prevChildren,nextChildren,container)</span><br><span class="line">                 <span class="keyword">break</span></span><br><span class="line">                <span class="keyword">case</span> childrenType.EMPTY:</span><br><span class="line">                    <span class="comment">//如果是老的vnode是单独的 新的是空的 就直接移除</span></span><br><span class="line">                    container.removeChild(prevChildren.el)</span><br><span class="line">                 <span class="keyword">break</span></span><br><span class="line">                <span class="keyword">case</span> childrenType.MULTIPLE:</span><br><span class="line">                    <span class="comment">//如果老的vnode 是单个 新的 是多个 先把老的干掉在 把新的循环挂载</span></span><br><span class="line">                    container.removeChild(prevChildren.el)</span><br><span class="line">                    <span class="keyword">for</span>(<span class="keyword">let</span> i = <span class="number">0</span>; i &lt; nextChildren.length; i++)&#123;</span><br><span class="line">                        mount(nextChildren[i],container)</span><br><span class="line">                    &#125;</span><br><span class="line">                 <span class="keyword">break</span></span><br><span class="line">            &#125;</span><br><span class="line">         <span class="keyword">break</span></span><br><span class="line">        <span class="comment">//老的虚拟DOM 是空的 然后分别取对比新的vnode那三种情况</span></span><br><span class="line">        <span class="keyword">case</span> childrenType.EMPTY:</span><br><span class="line">            <span class="keyword">switch</span>(nextChildFlag)&#123;</span><br><span class="line">                <span class="keyword">case</span> childrenType.SINGLE:</span><br><span class="line">                    <span class="comment">//如果老的vnode是空的 新的是单个 我们直接挂载就好</span></span><br><span class="line">                    mount(nextChildren,container)</span><br><span class="line">                 <span class="keyword">break</span></span><br><span class="line">                <span class="keyword">case</span> childrenType.EMPTY:</span><br><span class="line">                    <span class="comment">//如果老的vnode是空的 新的也是 那就啥也不干</span></span><br><span class="line">                 <span class="keyword">break</span></span><br><span class="line">                <span class="keyword">case</span> childrenType.MULTIPLE:</span><br><span class="line">                    <span class="comment">//如果新的vnode是空的 老的是多个 就循环的取挂载新的</span></span><br><span class="line">                    <span class="keyword">for</span>(<span class="keyword">let</span> i = <span class="number">0</span> ; i&lt; nextChildren.length;i++)&#123;</span><br><span class="line">                        mount(nextChildren[i],container)</span><br><span class="line">                    &#125;</span><br><span class="line">                 <span class="keyword">break</span></span><br><span class="line">            &#125;</span><br><span class="line">         <span class="keyword">break</span></span><br><span class="line">        <span class="comment">//老的虚拟DOM 是多个的 然后分别取对比新的vnode那三种情况</span></span><br><span class="line">        <span class="keyword">case</span> childrenType.MULTIPLE:</span><br><span class="line">            <span class="keyword">switch</span>(nextChildFlag)&#123;</span><br><span class="line">                <span class="keyword">case</span> childrenType.SINGLE:</span><br><span class="line">                    <span class="comment">//如果老的是多个 新的是单个 那就循环的去把老的vnode删掉 把新的挂载</span></span><br><span class="line">                    <span class="keyword">for</span>(<span class="keyword">let</span> i=<span class="number">0</span>;i&lt;prevChildren.length;i++)&#123;</span><br><span class="line">                        container.removeChild(prevChildren[i].el)</span><br><span class="line">                    &#125;</span><br><span class="line">                    mount(nextChildren,container)</span><br><span class="line">                 <span class="keyword">break</span></span><br><span class="line">                <span class="keyword">case</span> childrenType.EMPTY:</span><br><span class="line">                    <span class="comment">//如果新的vnode是多个 老的是空的 我们直接把老的删除掉即可</span></span><br><span class="line">                    <span class="keyword">for</span>(<span class="keyword">let</span> i=<span class="number">0</span>;i&lt;prevChildren.length;i++)&#123;</span><br><span class="line">                        container.removeChild(prevChildren[i].el)</span><br><span class="line">                    &#125;</span><br><span class="line">                 <span class="keyword">break</span></span><br><span class="line">                <span class="keyword">case</span> childrenType.MULTIPLE:</span><br><span class="line">                    <span class="comment">//如果老的vnode是多个 新的也就 就要重新对比了 老的vnode是数组 新的也是数组</span></span><br><span class="line">                    <span class="comment">//这里就设计到一些情况了</span></span><br><span class="line">                    <span class="comment">//这里抽象举例 老的是[a,b,c] 新的是[a,1,b,2,c,3] 这种情况</span></span><br><span class="line">                    <span class="comment">//因为新的abc 索引是递增的  而老的也是递增的 所以我们不需要做修改 只是新增它中间对应的元素 因为相对位置是不变的</span></span><br><span class="line">                    <span class="comment">//这我们所计算的都是相对位置 那么相对位置变和不变 我怎么辨别的 这里只要新的索引大于老的索引 我们就认为它的相对位置没有改变</span></span><br><span class="line">                    <span class="keyword">let</span> lastIndex = <span class="number">0</span></span><br><span class="line">                    </span><br><span class="line">                    <span class="keyword">for</span>(<span class="keyword">let</span> i= <span class="number">0</span>; i&lt; nextChildren.length;i++)&#123;</span><br><span class="line">                        <span class="comment">//把新的vnode都拿出来</span></span><br><span class="line">                        <span class="keyword">let</span> nextVnode = nextChildren[i]</span><br><span class="line">                        <span class="keyword">let</span> j = <span class="number">0</span>;</span><br><span class="line">                        <span class="keyword">let</span> find = <span class="literal">false</span></span><br><span class="line">                        <span class="keyword">for</span>(j;j&lt;prevChildren.length;j++)&#123;</span><br><span class="line">                            <span class="keyword">let</span> preVnode = prevChildren[j]</span><br><span class="line">                            <span class="keyword">if</span>(preVnode.key === nextVnode.key)&#123;</span><br><span class="line">                                <span class="comment">//如果找到了</span></span><br><span class="line">                                find = <span class="literal">true</span></span><br><span class="line">                                <span class="comment">//key相同 是同一个元素</span></span><br><span class="line">                                 patch(preVnode,nextVnode,container)</span><br><span class="line">                                </span><br><span class="line">                                 <span class="keyword">if</span>(j&lt;lastIndex)&#123;</span><br><span class="line">                                    <span class="comment">//需要移动节点 abc a移动到b之后 所以们要找到abc的父元素 insert一下</span></span><br><span class="line">                                    <span class="keyword">let</span> flagNode = nextChildren[i - <span class="number">1</span>].el.nextSibling</span><br><span class="line">                                    container.insertBefore(preVnode.el,flagNode)</span><br><span class="line">                                &#125;<span class="keyword">else</span>&#123;</span><br><span class="line">                                    lastIndex = j</span><br><span class="line">                                &#125;</span><br><span class="line">                            &#125;   </span><br><span class="line">                        &#125;</span><br><span class="line">                        <span class="keyword">if</span>(!find)&#123; </span><br><span class="line">                                <span class="comment">//如果老的vnode里没有找到这个节点 就说明是新增 </span></span><br><span class="line">                                <span class="keyword">let</span> flagNode = i == <span class="number">0</span> ? prevChildren[<span class="number">0</span>].el:nextChildren[i-<span class="number">1</span>].el.nextSibling</span><br><span class="line">                                mount(nextVnode,container,flagNode)</span><br><span class="line">                             &#125;</span><br><span class="line">                    &#125;</span><br><span class="line">                    <span class="comment">//移除不需要的元素 老的vnode里有 新的里面没有了 就要删除</span></span><br><span class="line">                    <span class="keyword">for</span>(<span class="keyword">let</span> i = <span class="number">0</span>; i&lt; prevChildren.length; i++)&#123;</span><br><span class="line">                        <span class="keyword">const</span> prevVnode = prevChildren[i]</span><br><span class="line">                        <span class="keyword">const</span> has = nextChildren.find(<span class="function"><span class="params">next</span> =&gt;</span> next.key == prevVnode.key)</span><br><span class="line">                        <span class="keyword">if</span>(!has)&#123;</span><br><span class="line">                            container.removeChild(prevVnode.el)</span><br><span class="line">                        &#125;</span><br><span class="line">                    &#125;</span><br><span class="line">                 <span class="keyword">break</span></span><br><span class="line">            &#125;</span><br><span class="line">         <span class="keyword">break</span></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="patchText方法"><a href="#patchText方法" class="headerlink" title="patchText方法"></a>patchText方法</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//如果你新的vnode直接就是个文本 那么我们就直接删除老的 在把文本挂载进去</span></span><br><span class="line"><span class="comment">//如果新的vnode 是个节点 </span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">patchText</span>(<span class="params">prev,next,container</span>) </span>&#123;</span><br><span class="line">    <span class="comment">//这里已经可以确认 你新的vnode 就是个文本节点了</span></span><br><span class="line">    <span class="keyword">let</span> el = (next.el = prev.el)</span><br><span class="line">    <span class="comment">//所以我们这里在判断如果你们的子节点都不一样 那么我就可以直接 把你新的DOM的文本直接复制即可</span></span><br><span class="line">    <span class="comment">//这里为什么要做判断 因为有可能 你的新的节点是这样 你好&lt;span&gt;啦啦啦&lt;/span&gt;是的我好</span></span><br><span class="line">    <span class="keyword">if</span>(next.children !== prev.children)&#123;</span><br><span class="line">        <span class="comment">//我们就直接让DOM的值等于你这个文本</span></span><br><span class="line">        el.nodeValue = next.children</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="测试调用"><a href="#测试调用" class="headerlink" title="测试调用"></a>测试调用</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line">&lt;script&gt;</span><br><span class="line">    <span class="comment">//刚才我们已经通过createElement函数生成了虚拟DOM</span></span><br><span class="line">    <span class="comment">//这些虚拟DOM 的节点上可能还有一些属性 </span></span><br><span class="line">    <span class="keyword">let</span> vnode = createElement(<span class="string">&#x27;div&#x27;</span>,&#123;<span class="attr">id</span>:<span class="string">&#x27;test&#x27;</span>&#125;,[</span><br><span class="line">        createElement(<span class="string">&#x27;p&#x27;</span>,&#123;<span class="attr">key</span>:<span class="string">&#x27;a&#x27;</span>,<span class="attr">style</span>:&#123;<span class="attr">color</span>:<span class="string">&#x27;red&#x27;</span>&#125;&#125;,<span class="string">&#x27;节点1&#x27;</span>),</span><br><span class="line">        createElement(<span class="string">&#x27;p&#x27;</span>,&#123;<span class="attr">key</span>:<span class="string">&#x27;b&#x27;</span>,<span class="string">&#x27;@click&#x27;</span>:<span class="function">() =&gt;</span> &#123;alert(<span class="string">&#x27;xx&#x27;</span>)&#125;&#125;,<span class="string">&#x27;节点2&#x27;</span>),</span><br><span class="line">        createElement(<span class="string">&#x27;p&#x27;</span>,&#123;<span class="attr">key</span>:<span class="string">&#x27;c&#x27;</span>,<span class="string">&#x27;class&#x27;</span>:<span class="string">&#x27;item-header&#x27;</span>&#125;,<span class="string">&#x27;节点3&#x27;</span>),</span><br><span class="line">        createElement(<span class="string">&#x27;p&#x27;</span>,&#123;<span class="attr">key</span>:<span class="string">&#x27;d&#x27;</span>&#125;,<span class="string">&#x27;节点4&#x27;</span>)</span><br><span class="line">    ])</span><br><span class="line">    <span class="keyword">let</span> vnode1 = createElement(<span class="string">&#x27;div&#x27;</span>,&#123;<span class="attr">id</span>:<span class="string">&#x27;test&#x27;</span>&#125;,[</span><br><span class="line">        createElement(<span class="string">&#x27;p&#x27;</span>,&#123;<span class="attr">key</span>:<span class="string">&#x27;d&#x27;</span>&#125;,<span class="string">&#x27;节点4&#x27;</span>),</span><br><span class="line">        createElement(<span class="string">&#x27;p&#x27;</span>,&#123;<span class="attr">key</span>:<span class="string">&#x27;a&#x27;</span>,<span class="attr">style</span>:&#123;<span class="attr">color</span>:<span class="string">&#x27;blue&#x27;</span>&#125;&#125;,<span class="string">&#x27;节点1&#x27;</span>),</span><br><span class="line">        createElement(<span class="string">&#x27;p&#x27;</span>,&#123;<span class="attr">key</span>:<span class="string">&#x27;b&#x27;</span>&#125;,<span class="string">&#x27;节点2&#x27;</span>),</span><br><span class="line">        createElement(<span class="string">&#x27;p&#x27;</span>,&#123;<span class="attr">key</span>:<span class="string">&#x27;c&#x27;</span>,<span class="string">&#x27;class&#x27;</span>:<span class="string">&#x27;item-header&#x27;</span>&#125;,<span class="string">&#x27;节点5&#x27;</span>),</span><br><span class="line">        createElement(<span class="string">&#x27;p&#x27;</span>,&#123;<span class="attr">key</span>:<span class="string">&#x27;e&#x27;</span>,<span class="attr">style</span>:&#123;<span class="attr">color</span>:<span class="string">&#x27;#bbb&#x27;</span>&#125;&#125;,<span class="string">&#x27;节点6&#x27;</span>)</span><br><span class="line">    ])</span><br><span class="line">    </span><br><span class="line">    <span class="comment">//在这里我们需要用render函数 把他们 都渲染到页面上</span></span><br><span class="line">    render(vnode,<span class="built_in">document</span>.querySelector(<span class="string">&#x27;#app&#x27;</span>))</span><br><span class="line">   </span><br><span class="line">        <span class="built_in">setTimeout</span>(<span class="function">() =&gt;</span> &#123;</span><br><span class="line">            render(vnode1,<span class="built_in">document</span>.querySelector(<span class="string">&#x27;#app&#x27;</span>))</span><br><span class="line">        &#125;,<span class="number">3000</span>)</span><br><span class="line"></span><br><span class="line">&lt;/script&gt;</span><br></pre></td></tr></table></figure>
<p><img src="2.gif" alt=""></p>
<h2 id="总结"><a href="#总结" class="headerlink" title="总结"></a>总结</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>首先就是我们的生成虚拟DOM的函数 createElement 然后会把你的父节点和子节点的类型都标记一下 比如是元素还是文本还是组件之类的</span><br><span class="line"><span class="number">2.</span>然后我们开始render函数 render函数有两种情况 一种是你首次要把你生成的虚拟DOM挂载到真实DOM上 一种是你原来有虚拟DOM 发生了更新 我们就要从新patch计算一下 然后在挂载</span><br><span class="line"><span class="number">3.</span>无论那种情况我们都要先把我们生成的虚拟DOM 存放一下</span><br><span class="line"><span class="number">4.</span>我们先说首次挂载 在render函数里调用的mount函数 需要把你的虚拟DOM 和你的容器传入进去</span><br><span class="line"><span class="number">5.</span>mount函数 接收三个参数 一个是你的虚拟DOM 一个是你的容器另外一个是 你新的虚拟DOM 需要改变位置的元素</span><br><span class="line"><span class="number">6.</span>这里我们挂载也是分为几种情况  我们先会判断你传进来的虚拟节点的类型 然后根据类型是标签还是文本 去执行不同的挂载方法</span><br><span class="line"><span class="number">7.</span>如果是标签 我们就mountElement 方法去挂载 首先你的标签名字在tag里面 我们根据这个去创建元素 然后把你的属性 子元素 和子元素节点都解构出来 然后在去判断你的子元素标记 在根据不同的标记情况 使用mount方法去挂载 </span><br><span class="line"><span class="number">7.</span>最后这里我们涉及到 如果你有更新的操作 比如 你有新的vnode位置发生改变 就要重新插入 或者 把你的元素挂载到容器上</span><br><span class="line"><span class="number">8.</span>第二种挂载就是你的元素是个文本 我们就调用 mountText 方法 然后直接创建你的文本直接挂载到DOM上</span><br><span class="line"><span class="number">9.</span>以上就是首次render函数里的挂载 然后 就是有新的vnode传进来 之后我们就需要patch 把新老的vnode对比一下 开始发生计算</span><br><span class="line"><span class="number">10.</span>render函数进来之后 我们会先判断 你的container.vnode在不在 如果在 就说名你是更新操作 我们就开调用patch函数 把你的新的vnode 和老的vnode 还有容器都传入进去</span><br><span class="line"><span class="number">11.</span>这个时候我们的patch函数对比 也分三种情况 首先会把你新老的vnode的标记搞出来 然后根据标记去判断 如果你老的vnode标记不等于新的vnode标记 就说明 你老的vnode 可能是个标签 新的vnode可是能是文本 这个时候我们就调用 replaceVnode 函数 把新老vnode都传入进去 因为你连标记类型都不一样了 我们这里在replaceVnode函数里 直接就把老的vnode移除 然后使用mount方法 挂载新的vnode</span><br><span class="line"><span class="number">12.</span>如果你新的vnode是个元素 老的也是 那么我们就调用 patchElement方法 去对比你的新老vnode </span><br><span class="line"><span class="number">13.</span>patchElement方法 因为都是标签都一样 我们这里只要对比一下属性即可 把属性替换一下 然后开始调 patchChildren 对比子元素的改变</span><br><span class="line"><span class="number">14.</span>patchChildren 方法涉及到三种情况 需要一次此的去对比 详见代码</span><br><span class="line"><span class="number">15.</span>最后的情况是如果你新传进来的是个文本 那么们就让你的老vnode 直接等于新的vnode文本即可</span><br></pre></td></tr></table></figure>
<h2 id="代码地址"><a href="#代码地址" class="headerlink" title="代码地址"></a>代码地址</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">代码地址:https:<span class="comment">//github.com/wazer1987/vDom</span></span><br></pre></td></tr></table></figure>
</article><div class="post-copyright"><div class="post-copyright__title"><span class="post-copyright-info"><h>虚拟Dom</h></span></div><div class="post-copyright__type"><span class="post-copyright-info"><a href="http://example.com/2020/04/18/%E8%99%9A%E6%8B%9FDom/">http://example.com/2020/04/18/%E8%99%9A%E6%8B%9FDom/</a></span></div><div class="post-copyright-m"><div class="post-copyright-m-info"><div class="post-copyright-a"><h>作者</h><div class="post-copyright-cc-info"><h>北城韩雨</h></div></div><div class="post-copyright-c"><h>发布于</h><div class="post-copyright-cc-info"><h>2020-04-18</h></div></div><div class="post-copyright-u"><h>更新于</h><div class="post-copyright-cc-info"><h>2019-06-03</h></div></div><div class="post-copyright-c"><h>许可协议</h><div class="post-copyright-cc-info"><a class="icon" rel="noopener" target="_blank" title="Creative Commons" href="https://creativecommons.org/"><i class="fab fa-creative-commons"></i></a><a rel="noopener" target="_blank" title="CC BY-NC-SA 4.0" href="https://creativecommons.org/licenses/by-nc-sa/4.0/">CC BY-NC-SA 4.0</a></div></div></div></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/%E8%99%9A%E6%8B%9FDom/">虚拟Dom</a></div><div class="post_share"></div></div><link rel="stylesheet" href="/" media="defer" onload="this.media='all'"/><div class="post-reward"><button class="tip-button reward-button"><span class="tip-button__text">打赏</span><div class="coin-wrapper"><div class="coin"><div class="coin__middle"></div><div class="coin__back"></div><div class="coin__front"></div></div></div><div class="reward-main"><ul class="reward-all"><li class="reward-item"><a href="/img/weixin.jpg" target="_blank"><img class="post-qr-code-img" src="/img/weixin.jpg" alt="微信"/></a><div class="post-qr-code-desc">微信</div></li><li class="reward-item"><a href="/img/taobao.jpg" target="_blank"><img class="post-qr-code-img" src="/img/taobao.jpg" alt="支付宝"/></a><div class="post-qr-code-desc">支付宝</div></li></ul></div></button></div><script defer="defer" src="/"></script><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/2020/04/18/%E7%AE%97%E6%B3%95/"><img class="prev-cover" src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic.vjshi.com%2F2016-12-29%2F0604092c7129acd0f4029c6728db7fd7%2F00001.jpg%3Fx-oss-process%3Dstyle%2Fwatermark&amp;refer=http%3A%2F%2Fpic.vjshi.com&amp;app=2002&amp;size=f9999,10000&amp;q=a80&amp;n=0&amp;g=0n&amp;fmt=jpeg?sec=1629530724&amp;t=333c5056a813a542419e5b661ebcfcd9" onerror="onerror=null;src='/img/404.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">常用算法及思路</div></div></a></div><div class="next-post pull-right"><a href="/2020/04/14/render%E5%87%BD%E6%95%B0%20%E5%87%BD%E6%95%B0%E5%BC%8F%E7%BB%84%E4%BB%B6%20JSX/"><img class="next-cover" src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic.vjshi.com%2F2018-12-05%2F66b696ea615f3567a32b65f93611a814%2F00004.jpg%3Fx-oss-process%3Dstyle%2Fwatermark&amp;refer=http%3A%2F%2Fpic.vjshi.com&amp;app=2002&amp;size=f9999,10000&amp;q=a80&amp;n=0&amp;g=0n&amp;fmt=jpeg?sec=1629530687&amp;t=c2e3c97de6dbdc29e77593d5f4dc7fb5" onerror="onerror=null;src='/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">Vue中的函数式组件</div></div></a></div></nav></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src="/img/my.jpg" onerror="this.onerror=null;this.src='/img/friend_404.gif'" alt="avatar"/></div><div class="author-info__name">北城韩雨</div><div class="author-info__description">一个前端界的小学生</div></div><div class="card-info-data"><div class="card-info-data-item is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">65</div></a></div><div class="card-info-data-item is-center"><a href="/tags/"><div class="headline">标签</div><div class="length-num">23</div></a></div><div class="card-info-data-item is-center"><a href="/categories/"><div class="headline">分类</div><div class="length-num">21</div></a></div></div><a class="button--animated" id="card-info-btn" target="_blank" rel="noopener" href="https://github.com/wazer1987"><i class="fab fa-github"></i><span>Follow Me</span></a><div class="card-info-social-icons is-center"><a class="social-icon" href="/wangzhengvslike@163.com" target="_blank" title="Email"><i class="fas fa-envelope"></i></a></div></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn card-announcement-animation"></i><span>公告</span></div><div class="announcement_content"><p>感谢访问本站，如喜欢请收藏。本站主要分享前端知识，立志成为资深前端工程师，但目前是一个前端界的小学生 若喜欢可以打赏请博主喝一杯冰阔落</p> <p>另外请大家多多支持淼哥的开源项目</p> <p>https://github.com/flipped-aurora/gin-vue-admin</p> <p>🌟🌟欢迎大家start 🌟🌟</p> <p>欢迎加入博主的前端技术交流群</p> <p style="background:rgba(255,255,255,1);margin:0"> <img src="/img/code.png"></img> </p></div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#%E8%99%9A%E6%8B%9FDOM-Vdom"><span class="toc-text">虚拟DOM  Vdom</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E8%99%9A%E6%8B%9FDOM-VDom-%E6%98%AF%E4%BB%80%E4%B9%88"><span class="toc-text">虚拟DOM(VDom)是什么</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%A6%82%E4%BD%95%E6%96%B0%E5%BB%BA%E8%99%9A%E6%8B%9FDOM"><span class="toc-text">如何新建虚拟DOM</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%A6%82%E4%BD%95%E6%B8%B2%E6%9F%93%E8%99%9A%E6%8B%9FDOM"><span class="toc-text">如何渲染虚拟DOM</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%A4%84%E7%90%86%E8%8A%82%E7%82%B9%E7%9A%84%E5%B1%9E%E6%80%A7"><span class="toc-text">处理节点的属性</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#render%E5%87%BD%E6%95%B0%E7%9A%84%E6%B8%B2%E6%9F%93%E9%80%BB%E8%BE%91"><span class="toc-text">render函数的渲染逻辑</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%9B%B4%E6%96%B0patch-diff"><span class="toc-text">更新patch diff</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#patch%E6%96%B9%E6%B3%95"><span class="toc-text">patch方法</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#replaceVnode%E6%96%B9%E6%B3%95"><span class="toc-text">replaceVnode方法</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#patchElement%E6%96%B9%E6%B3%95"><span class="toc-text">patchElement方法</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#patchChildren%E6%96%B9%E6%B3%95"><span class="toc-text">patchChildren方法</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#patchText%E6%96%B9%E6%B3%95"><span class="toc-text">patchText方法</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%B5%8B%E8%AF%95%E8%B0%83%E7%94%A8"><span class="toc-text">测试调用</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%80%BB%E7%BB%93"><span class="toc-text">总结</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%BB%A3%E7%A0%81%E5%9C%B0%E5%9D%80"><span class="toc-text">代码地址</span></a></li></ol></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/2021/04/16/gin-vue-admin/" title="Gin-Vue-Admin"><img src="/img/gin-vue-admin.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Gin-Vue-Admin"/></a><div class="content"><a class="title" href="/2021/04/16/gin-vue-admin/" title="Gin-Vue-Admin">Gin-Vue-Admin</a><time datetime="2021-04-15T16:00:00.000Z" title="发表于 2021-04-16 00:00:00">2021-04-16</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2021/01/08/Vue%E6%BA%90%E7%A0%81%E5%BC%80%E5%8F%91%E4%B9%8B%E7%BB%84%E4%BB%B6%E7%9A%84%E6%B8%B2%E6%9F%9310/" title="Vue源码开发-组件的渲染10"><img src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic.vjshi.com%2F2018-07-17%2F3eca5394963b50e37b7be2ba320029ec%2F00003.jpg%3Fx-oss-process%3Dstyle%2Fwatermark&amp;refer=http%3A%2F%2Fpic.vjshi.com&amp;app=2002&amp;size=f9999,10000&amp;q=a80&amp;n=0&amp;g=0n&amp;fmt=jpeg?sec=1629530644&amp;t=10679b5732c52e39eeb588040240a790" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Vue源码开发-组件的渲染10"/></a><div class="content"><a class="title" href="/2021/01/08/Vue%E6%BA%90%E7%A0%81%E5%BC%80%E5%8F%91%E4%B9%8B%E7%BB%84%E4%BB%B6%E7%9A%84%E6%B8%B2%E6%9F%9310/" title="Vue源码开发-组件的渲染10">Vue源码开发-组件的渲染10</a><time datetime="2021-01-07T16:00:00.000Z" title="发表于 2021-01-08 00:00:00">2021-01-08</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2021/01/07/Vue%E6%BA%90%E7%A0%81%E5%BC%80%E5%8F%91%E4%B9%8B%E7%BB%84%E4%BB%B6%E7%9A%84%E5%90%88%E5%B9%B6%E7%AD%96%E7%95%A509/" title="Vue源码开发-组件合并策略09"><img src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fcz-video-photo.oss-cn-beijing.aliyuncs.com%2F20191017%2F4cfc341591d242327b67e9ceb6953612_00002.jpg&amp;refer=http%3A%2F%2Fcz-video-photo.oss-cn-beijing.aliyuncs.com&amp;app=2002&amp;size=f9999,10000&amp;q=a80&amp;n=0&amp;g=0n&amp;fmt=jpeg?sec=1629530702&amp;t=80943e12d5239f63beafd6d1c540855f" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Vue源码开发-组件合并策略09"/></a><div class="content"><a class="title" href="/2021/01/07/Vue%E6%BA%90%E7%A0%81%E5%BC%80%E5%8F%91%E4%B9%8B%E7%BB%84%E4%BB%B6%E7%9A%84%E5%90%88%E5%B9%B6%E7%AD%96%E7%95%A509/" title="Vue源码开发-组件合并策略09">Vue源码开发-组件合并策略09</a><time datetime="2021-01-06T16:00:00.000Z" title="发表于 2021-01-07 00:00:00">2021-01-07</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2021/01/06/Vue%E6%BA%90%E7%A0%81%E5%BC%80%E5%8F%91%E4%B9%8B%E6%95%B0%E7%BB%84%E7%9A%84%E4%BE%9D%E8%B5%96%E6%94%B6%E9%9B%8608/" title="Vue源码开发-数组的依赖收集08"><img src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic.vjshi.com%2F2018-07-17%2F3eca5394963b50e37b7be2ba320029ec%2F00003.jpg%3Fx-oss-process%3Dstyle%2Fwatermark&amp;refer=http%3A%2F%2Fpic.vjshi.com&amp;app=2002&amp;size=f9999,10000&amp;q=a80&amp;n=0&amp;g=0n&amp;fmt=jpeg?sec=1629530644&amp;t=10679b5732c52e39eeb588040240a790" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Vue源码开发-数组的依赖收集08"/></a><div class="content"><a class="title" href="/2021/01/06/Vue%E6%BA%90%E7%A0%81%E5%BC%80%E5%8F%91%E4%B9%8B%E6%95%B0%E7%BB%84%E7%9A%84%E4%BE%9D%E8%B5%96%E6%94%B6%E9%9B%8608/" title="Vue源码开发-数组的依赖收集08">Vue源码开发-数组的依赖收集08</a><time datetime="2021-01-05T16:00:00.000Z" title="发表于 2021-01-06 00:00:00">2021-01-06</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2021/01/06/Vue%E6%BA%90%E7%A0%81%E5%BC%80%E5%8F%91%E4%B9%8B%E7%94%9F%E5%91%BD%E5%91%A8%E6%9C%9F%E5%90%88%E5%B9%B607/" title="Vue源码开发-生命周期合并07"><img src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic.vjshi.com%2F2018-07-17%2Faf41dabad727ba80c9ca8a2bfbd22a58%2F00004.jpg%3Fx-oss-process%3Dstyle%2Fwatermark&amp;refer=http%3A%2F%2Fpic.vjshi.com&amp;app=2002&amp;size=f9999,10000&amp;q=a80&amp;n=0&amp;g=0n&amp;fmt=jpeg?sec=1629530659&amp;t=bbcfe1e86f393eecbb0722db1b92e475" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Vue源码开发-生命周期合并07"/></a><div class="content"><a class="title" href="/2021/01/06/Vue%E6%BA%90%E7%A0%81%E5%BC%80%E5%8F%91%E4%B9%8B%E7%94%9F%E5%91%BD%E5%91%A8%E6%9C%9F%E5%90%88%E5%B9%B607/" title="Vue源码开发-生命周期合并07">Vue源码开发-生命周期合并07</a><time datetime="2021-01-05T16:00:00.000Z" title="发表于 2021-01-06 00:00:00">2021-01-06</time></div></div></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="copyright"><span>&copy;2017 - 2021</span><svg style="width:2em; height:2em;position: relative;top: 10px;" aria-hidden="true"><use xlink:href="#icon-Butterfly"></use></svg><span>北城韩雨</span></div><div class="framework-info"><span>框架 </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>主题 </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div><div class="footer_custom_text"><p><a style="margin-inline:5px" target="_blank" href="https://hexo.io/"><img src="https://img.shields.io/badge/Frame-Hexo-blue?style=flat&logo=hexo" title="博客框架为Hexo"></a><a style="margin-inline:5px" target="_blank" href="https://butterfly.js.org/"><img src="https://img.shields.io/badge/Theme-Butterfly-6513df?style=flat&logo=bitdefender" title="主题采用butterfly"></a><a style="margin-inline:5px" target="_blank" href="https://www.jsdelivr.com/"><img src="https://img.shields.io/badge/CDN-jsDelivr-orange?style=flat&logo=jsDelivr" title="本站使用JsDelivr为静态资源提供CDN加速"></a></p></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="回到顶部"><i class="fas fa-arrow-up"></i></button></div></div><div><script src="/js/utils.js"></script><script src="/js/main.js"></script><script src="https://cdn.jsdelivr.net/npm/medium-zoom/dist/medium-zoom.min.js"></script><div class="js-pjax"></div><script defer src="/live2d-widget/autoload.js"></script><script src="/js/coin.js"></script><script async src="//at.alicdn.com/t/font_2700632_3rtvhzzpl3h.js"></script><script src="/js/liuxing.js" data-pjax></script><canvas class="fireworks" mobile="false"></canvas><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/fireworks.min.js"></script><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/activate-power-mode.min.js"></script><script>POWERMODE.colorful = true;
POWERMODE.shake = true;
POWERMODE.mobile = false;
document.body.addEventListener('input', POWERMODE);
</script><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div><!-- hexo injector body_end start --><div class="js-pjax"><script async="async">var arr = document.getElementsByClassName('recent-post-item');
for(var i = 0;i<arr.length;i++){
    arr[i].classList.add('wow');
    arr[i].classList.add('animate__zoomIn');
    arr[i].setAttribute('data-wow-duration', '1s');
    arr[i].setAttribute('data-wow-delay', '0ms');
    arr[i].setAttribute('data-wow-offset', '100');
    arr[i].setAttribute('data-wow-iteration', '1');
  }</script><script async="async">var arr = document.getElementsByClassName('card-widget');
for(var i = 0;i<arr.length;i++){
    arr[i].classList.add('wow');
    arr[i].classList.add('animate__zoomIn');
    arr[i].setAttribute('data-wow-duration', '');
    arr[i].setAttribute('data-wow-delay', '');
    arr[i].setAttribute('data-wow-offset', '');
    arr[i].setAttribute('data-wow-iteration', '');
  }</script></div><script defer src="https://cdn.jsdelivr.net/gh/graingert/wow@1.3.0/dist/wow.min.js"></script><script defer src="https://cdn.jsdelivr.net/npm/hexo-butterfly-wowjs/lib/wow_init.min.js"></script><script async src="//at.alicdn.com/t/font_2032782_8d5kxvn09md.js"></script><!-- hexo injector body_end end --></body></html>